package cn.smbms.controller.provider;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/jsp/provider")
public class ProviderController {

	@Autowired
	ProviderService providerService;

	@RequestMapping("/delprovider")
	@ResponseBody
	private String delProvider(String proid) {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(proid)){
			int flag = providerService.deleteProviderById(proid);
			if(flag == 0){//删除成功
				resultMap.put("delResult", "true");
			}else if(flag == -1){//删除失败
				resultMap.put("delResult", "false");
			}else if(flag > 0){//该供应商下有订单，不能删除，返回订单数
				resultMap.put("delResult", String.valueOf(flag));
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		//把resultMap转换成json对象输出
		return  JSONArray.toJSONString(resultMap);
	}
	@RequestMapping("/modifysave")
	private String  modify(@ModelAttribute Provider provider,HttpSession session) {
		provider.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setModifyDate(new Date());

		boolean flag = false;
		
		flag = providerService.modify(provider);
		if(flag){
			return "redirect:/jsp/provider/query";
		}else{
			return "providermodify";
		}
	}

	@RequestMapping("/{url}/{proid}")
	private String  getProviderById(@PathVariable("url") String url ,@PathVariable("proid") String proid,Model model) {
		if(!StringUtils.isNullOrEmpty(proid)){
			
			Provider provider = null;
			provider = providerService.getProviderById(proid);
			model.addAttribute("provider", provider);
			return url;
		}else {
			return "error";
		}
	}
	@RequestMapping("/add")
	private String  add(@ModelAttribute Provider provider, HttpSession session) {
		provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setCreationDate(new Date());
		boolean flag = false;
		
		flag = providerService.add(provider);
		if(flag){
			return  "redirect:/jsp/provider/query";
		}else{
			return "provideradd";
		}
	}

	@RequestMapping("/query")
	private String  query(String queryProName, String queryProCode, Model model,@RequestParam(value = "pageIndex",defaultValue = "0")String pageIndex) {

		//设置页面容量
		int pageSize = Constants.pageSize;
		//当前页码
		int currentPageNo = 1;

		if(pageIndex != null){
			try{
				currentPageNo = Integer.valueOf(pageIndex);
			}catch(NumberFormatException e){
				return "error" ;
			}
		}
		//总数量（表）
		int totalCount	= providerService.getcount(queryProName,queryProCode);
		//总页数
		PageSupport pages=new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		//控制首页和尾页
		if(currentPageNo < 1){
			currentPageNo = 1;
		}else if(currentPageNo > totalPageCount){
			currentPageNo = totalPageCount;
		}
		if(StringUtils.isNullOrEmpty(queryProName)){
			queryProName = "";
		}
		if(StringUtils.isNullOrEmpty(queryProCode)){
			queryProCode = "";
		}
		List<Provider> providerList = new ArrayList<Provider>();
		
		providerList = providerService.getProviderList(queryProName,queryProCode,currentPageNo,pageSize);
		model.addAttribute("providerList", providerList);
		model.addAttribute("queryProName", queryProName);
		model.addAttribute("queryProCode", queryProCode);

		model.addAttribute("totalPageCount",totalPageCount);
		model.addAttribute("totalCount",totalCount);
		model.addAttribute("currentPageNo",currentPageNo);
		return "providerlist";

	}
	

}
